<?php
/**
 * 
 * 订单信息
 * xysc users Version1.0
 * ==================================
 * Copy 2016-2017
 * Web:www.xjh7.com
 * ==================================
 * Author:xjh
 * Date:2016-4-24
 * model() 创建模型对象，静态
 * tableName() 返回表名
*/
class Order extends CActiveRecord {
	public $page_list = array();//分页数组
	public $infos_count = 0;//统计查询个数
	/**
	 *返回当前模型对象的方法 
	 */
	public static function model($className = __CLASS__){
		return parent::model($className);
	}
	/**
	 * 返回当前表名
	 */
	public function tableName(){
		return '{{order}}';
	}
	
	/**
	 * 根据订单号查询单条记录
	 */
	public function getBrandByOrder_no(){
		
	}

	
	/**
	 * 分页+搜索
	 */
	public function pageSearch($user_id,$order_no,$order_status){
		$cmodel = new CCCModel();
		
		
		$user_model = new LoginForm();
		$role_name = $user_model -> getRoleName();
		$role_name = $role_name[0]['role_name'];
		if($role_name != '管理员'){
			$_sql = "SELECT a.order_no,b.order_status_id,a.active,a.express_id,a.order_sort,b.order_fee,c.goods_name,b.goods_nums,b.goods_id FROM {{order}} a,{{order_details}} b,{{goods}} c WHERE a.order_no=b.order_no AND b.goods_id=c.goods_id AND c.goods_id IN (SELECT e.goods_id FROM xy_store d,xy_goods_store e WHERE d.store_owner=$user_id AND e.store_id=d.store_id )";
		}else{
			$_sql = "SELECT a.order_no,b.order_status_id,a.active,a.express_id,a.order_sort,b.order_fee,c.goods_name,b.goods_nums,b.goods_id FROM {{order}} a,{{order_details}} b,{{goods}} c WHERE a.order_no=b.order_no AND b.goods_id=c.goods_id AND c.goods_id IN (SELECT e.goods_id FROM xy_store d,xy_goods_store e WHERE  e.store_id=d.store_id )";
		}
		$nextpage = "";
		if (isset($order_no)){    
			if($order_status != 999){
				$nextpage .= "&order_status_id=".$order_status;
				$_sql.=" AND b.order_status_id = $order_status ";
			} 
			if($order_no != null){
				$nextpage .= "&order_no=".$order_no;
				$_sql.=" AND b.order_no like '%".$order_no."%' ";
			}
			$_sql_count = str_replace('SELECT *','SELECT count(*)',$_sql);
		}

		$infos = $cmodel->sel($_sql);
		

			/**
			 * 分页
			 
			$per = 8;
			$this->infos_count = $cmodel->sel($_sql_count);
			$cnt = $this->infos_count[0]['count(*)'];
			$page = new Pagination($cnt,$per,$nextpage);
			$_sql .= " ORDER BY brand_sort ASC $page->limit";
			$infos = $cmodel->sel($_sql);
			$page_list[0] = $page->fpage(array(3));
			$page_list[1] = $page->fpage(array(4));
			$page_list[2] = $page->fpage(array(5));
			$page_list[3] = $page->fpage(array(6));
			$page_list[4] = $page->fpage(array(7));
			
			$this->page_list[0] = $page_list[0];
			$this->page_list[1] = $page_list[1];
			$this->page_list[2] = $page_list[2];
			$this->page_list[3] = $page_list[3];
			$this->page_list[4] = $page_list[4];
			*/
			return $infos;
	}
	

	/**
	 *商家更新订单信息(包括实付款和排序)
	 */
	public function updOrder($order_no,$goods_id,$order_status,$price){
		$cmodel = new CCCModel();
		$_sql = "UPDATE {{order_details}} SET order_fee = $price,order_status_id=$order_status WHERE order_no ='".$order_no."' AND goods_id = $goods_id";
		$row = $cmodel->upd($_sql);
		return $row;
	}


	/**
	 * 按照sort查询所有数据
	 */
	public function all($user_id){
		$all_model = new CCCModel();

		$user_model = new LoginForm();
		$role_name = $user_model -> getRoleName();
		$role_name = $role_name[0]['role_name'];
		if($role_name != '管理员'){
			$_sql = "SELECT a.order_no,a.active,a.express_id,a.order_sort,b.order_fee,c.goods_name,b.goods_nums,b.goods_id,b.order_status_id FROM {{order}} a,{{order_details}} b,{{goods}} c WHERE a.order_no=b.order_no AND b.goods_id=c.goods_id AND c.goods_id IN (SELECT e.goods_id FROM xy_store d,xy_goods_store e WHERE d.store_owner=$user_id AND e.store_id=d.store_id )";
		}else{
			$_sql = "SELECT a.order_no,a.active,a.express_id,a.order_sort,b.order_fee,c.goods_name,b.goods_nums,b.goods_id,b.order_status_id FROM {{order}} a,{{order_details}} b,{{goods}} c WHERE a.order_no=b.order_no AND b.goods_id=c.goods_id AND c.goods_id IN (SELECT e.goods_id FROM xy_store d,xy_goods_store e WHERE  e.store_id=d.store_id )";
		}
		$result = $all_model->sel($_sql);
		return $result;
	}
	public function allOrder(){
		$all_model = new CCCModel();
		$_sql = "SELECT * FROM {{order_details}} a,{{goods}} b WHERE a.goods_id=b.goods_id";
		$result = $all_model->sel($_sql);
		return $result;
	}

	/**
	 *获得交易状态
	 */
	public function getOrderStatus(){
		$cmodel = new CCCModel();
		$_sql = "SELECT * FROM {{order_status}}";
		$result = $cmodel->sel($_sql);
		return $result;
	}
	
	/**
	 * 获得所有快递信息
	 */
	public function getAllExpress(){
		$cmodel = new CCCModel();
		$_sql = "SELECT * FROM {{express}}";
		$result = $cmodel->sel($_sql);
		return $result;
	}
	/**
	 * 按照sort排序
	 * @param unknown_type $_data
	 */
	public function sort($_data){
		$cmodel = new CCCModel();
		foreach($_data as $_k => $_v){
			$_sql = "UPDATE xy_brand SET brand_sort=".$_v." WHERE brand_id=".$_k;
			$a = $cmodel->upd($_sql);
		}
	}
}
	
?>